<template>
	<div class="goodsListcontainer">
        <!-- 顶部筛选菜单和搜索框 -->
		<div class="topBox">
			<el-select v-model="searchType" placeholder="Select" @change="changeSearch">
				<el-option label="全部" value="-1"/>
				<el-option label="审核中" value="0"/>
				<el-option label="审核失败" value="1"/>
				<el-option label="销售中" value="2"/>
				<el-option label="下架" value="3"/>
				<el-option label="删除" value="4"/>
			</el-select>
			<el-input v-model="keyword" placeholder="搜索商品名称" clearable maxlength="50"/>
			<el-button type="primary" icon="Search" @click="getList(2)">搜索</el-button>
		</div>
        <!-- 表格 -->
		<el-table :data="list" border>
			<el-table-column prop="product_id" label="商品id" align="center" min-width="80"></el-table-column>
			<el-table-column prop="product_name" label="商品名称" align="center" min-width="140" show-overflow-tooltip></el-table-column>
			<el-table-column prop="store_name" label="店铺" align="center" min-width="100"></el-table-column>
			<el-table-column prop="like" label="收藏" align="center" min-width="100"></el-table-column>
			<el-table-column label="状态" align="center" min-width="100">
                <!-- 子组件中没有给插槽命名，就自动隐式命名为default 而#为v-slot:的简写
                所以这里的意思是将tempalte包裹的这部分模板传入子组件的default插槽中。
                "scope"作为形参是子组件传递过来的数据，以对象的形式接收过来 -->
				<template #default="scope">
					<el-tag :type="scope.row.status==2?'success':scope.row.status==0?'info':scope.row.status==1?'warning':'danger'">
						{{ scope.row.status==0?'审核中':scope.row.status==1?'审核失败':scope.row.status==2?'销售中':scope.row.status==3?'下架':'删除'}}
					</el-tag>
				</template>
			</el-table-column>
			<el-table-column prop="create_time" label="发布时间" align="center" min-width="180"></el-table-column>
			<el-table-column prop="update_time" label="更新时间" align="center" min-width="180"></el-table-column>
			<el-table-column label="操作" min-width="180" align="center">
				<template #default="scope">
				   <div class="btn">
					<el-button  icon="Edit" @click="handleEdit(scope.row)" :disabled="isAdmin=='0'">编辑</el-button>
					<!-- <el-button  icon="Setting" @click="examine(scope.row,scope.$index)" style="color:#5c9a2c" v-if="scope.row.status==0">审核</el-button> -->
					<el-button  icon="Setting" @click="examine(scope.row,scope.$index)" style="color:#5c9a2c" v-if="isAdmin=='1'">审核</el-button>
					<el-button  icon="Setting" @click="changeStatus(scope.row,scope.$index,2)" style="color:#5c9a2c" v-if="isAdmin=='1'">上架</el-button>
					<el-button  icon="Setting" @click="changeStatus(scope.row,scope.$index,3)" style="color:#5c9a2c" v-if="isAdmin=='1'">恢复</el-button>
					<el-button  icon="Delete" @click="handleDelete(scope.row.product_id,scope.$index)" style="color:#ff4141" v-if="isAdmin=='1'">删除</el-button>
				   </div>
				</template>
			</el-table-column>
		</el-table>
		<div class="pagination">
			<el-pagination hide-on-single-page background layout="total, prev, pager, next" :current-page="page" :page-size="row" :total="records" :page-count="total" @current-change="handlePageChange"/>
		</div>
		
		<!-- 审核 -->
		<el-dialog title="审核" v-model="shenHe" width="380px">
			<div class="option">
				<el-radio v-model="isable" :label="true" size="default" border>通过</el-radio>
				<el-radio v-model="isable" :label="false" size="default" border>不通过</el-radio>
			</div>
		     <template #footer>
		         <div class="el-dialog__footer">
		             <el-button @click="shenHe = false">取 消</el-button>
		             <el-button type="primary" @click="confirm">确 定</el-button>
		         </div>
		     </template>
		 </el-dialog>
	</div>
</template>

<script setup>
import { ref } from '@vue/reactivity'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getGoodList,changeGoodStatus,checkIsAdmin } from '../../api/manager'
import { useRouter } from 'vue-router'
import { userStore } from '../../store'
const router = useRouter()
const uStore = userStore()

let searchType = ref('-1')
let page = ref(1)
let row = ref(10)
let records = ref(1)
let total = ref(1)
let keyword = ref('')
let value = ref('')
let shenHe = ref(false)
let isable = ref(true)
let activeObj=ref({
	index:0,
	data:{}
})

const list = ref([])
// let data = {
//     page:1,
//     row:10,
//     keyword:''
// }
// getGoodList(data).then(res=>{
// 	list.value = res.data
// }).catch(err=>console.log(err))


//获取用户的管理员身份 0=>普通管理员 1=>超级管理员
const isAdmin = ref('0')
const userId = JSON.parse(sessionStorage.getItem('userInfo'))?.user_id || ''
checkIsAdmin({user_id:userId}).then(res=>{
    console.log('checkIsAdmin',res)
    isAdmin.value = res.data
})

//获取列表
const getList = (e) =>{
	//e==2说明是点击搜索来获取的
	if(e==2){
		page.value = 1
	}
	let data = {
		page:page.value,
		row:row.value,
		keyword:keyword.value
	}
	if(searchType.value > -1){//说明搜索的不是全部
		data.status=searchType.value
	} 
	getGoodList(data).then(res=>{
		if(res.code == 200){
			list.value = res.data
			records.value = res.records
			total.value = res.total
		}else{
			ElMessage.error(res.msg)
		}
	})

}
getList()

//选择器发生变化
const changeSearch = (value) =>{
	getList(2)
}

//编辑
const handleEdit = (item) =>{
	// router.push('/goods/add?id='+item.product_id)
	router.push({
		name:'goodAdd',
		params:{id:item.product_id}
	})
}
//审核
const examine = (item,index) =>{
	//需要超级管理员身份
	if(isAdmin.value == '0'){
		ElMessage.error('您不是超级管理员，没有操作权限！')
	}else{
		activeObj.value.item = item
		activeObj.value.index= index
		shenHe.value = true
	}
}

//审核确定
const confirm = () =>{
	//需要超级管理员身份
	if(isAdmin.value == '0'){ return ElMessage.error('您不是超级管理员，没有操作权限！')}
	changeGoodStatus({
		product_id:activeObj.value.item.product_id,
		status:isable.value?2:1
	}).then(res=>{
		if(res.code == 200){
			list.value[activeObj.value.index].status = res.data.status
			shenHe.value = false
		}else{
			ElMessage.error(res.msg)
		}
	})
	
}
//上架/恢复
const changeStatus = (item,index,type) =>{
	//需要超级管理员身份
	if(isAdmin.value == '0'){ return ElMessage.error('您不是超级管理员，没有操作权限！')}
	changeGoodStatus({
		product_id:item.product_id,
		status:type
	}).then(res=>{
		if(res.code == 200){
			list.value[index].status = res.data.status
		}else{
			ElMessage.error(res.msg)
		}
	})
}
//删除
const handleDelete = () =>{
	//需要超级管理员身份
	if(isAdmin.value == '0'){ return ElMessage.error('您不是超级管理员，没有操作权限！')}
	ElMessageBox.confirm('确定要删除吗？','提示',{
		confirmButtonText:'确定',
		cancelButtonText:'取消',
		type:'warning',
		buttonSize:'default'
	}).then(()=>{
		changeGoodStatus({
			product_id:id,
			status:4
		}).then((res)=>{
			if(res.code == 200){
				list.value[index].status = res.data.status
			}else{
				ElMessage.error(res.msg)
			}
		})
	}).catch((err)=>{
		ElMessage.error(err)
	})
}

//换页
const handlePageChange = (value) =>{
	page.value = value
	getList()
}


</script>


<style lang="less" scoped>
.goodsListcontainer {
	.btn{
		display: flex;
	}
		.topBox{
			display: flex;
			align-items: center;
			margin: 0 0 15px;
			.el-input{
				width: 200px;
			}
			.el-input__inner{
				border-radius: 0;
			}
			.el-button{
				border-radius: 0 3px 3px 0;
			}
			.el-select {
				width: 100px;
				.el-input{
					width: 100px;
					input{
						border-radius: 3px 0 0 3px;
					}
				}
			}
		}
		.el-table{
			margin:20px 0 0;
		}
	}
</style>